from os import name
import pickle
import urllib.request
import json
import gzip


pickle_file = open('D:\\PythonWorkSpaces\\city_data.pkl','rb')
city = pickle.load(pickle_file)
pickle_file.close()

# city_name=input('请输入城市:')
city_name = '长沙'
password=city_name
name1=city[password]

url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
# url = 'http://m.weather.com.cn/data/{password}.html'.format(password=name1)
url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey={password}'.format(password=name1)
# 网址1只需要输入城市名，网址2需要输入城市代码
File1 = urllib.request.urlopen(url2).read()
# 读取网页数据
weatherHTML = gzip.decompress(File1).decode('utf-8')
# 解压网页数据
weather_dict = json.loads(weatherHTML)
# 将json数据转换为dict数据
if weather_dict.get('desc') == 'invilad-citykey':
    print("xing", "你输入的城市名有误，或者天气中心未收录你所在城市")
else:
    # print(weather_dict, city_name)
    # print(weather_dict)
    data = weather_dict.get("data")
    now = data.get('forecast')
    # print(list(now))
    # #打印信息
    
    print ( '城市：', data['city'])
    print ('时间：', now[0].get('date'))
    print ( '24小时天气：')
    print ('温度：', data['wendu'])
    print ('天气：', now[0].get('type'))
    fengsu = now[0].get('fengli')
    fengsu = fengsu[9:11]
    print ('风速：', fengsu)
    print ('最低温度：', now[0].get('low'))
    print ('最高温度：', now[0].get('high'))
    print ('风向：', now[0].get('fengxiang'))
# input ('按任意键退出：')